<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// ------------------------------------------------------------------------

/**
 * Achievements_model class
 *
 * This class abstracts functions for the photo requests
 *
 * @package		Photoggle
 * @subpackage	Models
 * @category	Models
 * @author		Russ
 */
class TodoAchievements_model extends CI_Model {

	/* Achievement bahavior */
	var $table_name = 'a_achievements_todo';

	function __construct()
	{
		parent::__construct();
	}


		/**
	 * Creates a new todo achievement
	 */
	function create_todo_achievement($achievementId, $userId)
	{
		$data = array('achievement_id' => $achievementId, 'user_id' => $userId);
		if ($this->db->insert($this->table_name, $data)) {
			return $this->db->insert_id();
		}
		return NULL;
	}
	
	/**
	 * Fetch a users todo achievement list
	 *
	 * @access	public
	 * @return	array
	 */
	function get_todo_achievements($user_id)
	{
		$sql = "
				SELECT
					a_achievements.id 			  AS achievementId,
					a_achievements.category_id 	  AS achievementCategoryId,
					a_achievements.points 		  AS achievementPoints,
					a_achievements.title 		  AS achievementTitle,
					a_achievements.description	  AS achievementDescription,
					a_achievements.date_created   AS achievementDateCreated,
					a_achievement_categories.name AS achievementCategoryName
				FROM
					a_achievements_todo
				JOIN
					a_achievements ON a_achievements.id=a_achievements_todo.achievement_id
				JOIN
					a_achievement_categories ON a_achievement_categories.id=a_achievements.category_id
				WHERE
					a_achievements_todo.user_id = " . $user_id . "
				ORDER BY 
					a_achievements_todo.date_added
				DESC
				";
		$query = $this->db->query($sql);
		return $query->result();
	}
	
	/**
	 * Count the TODO Achievements
	 *
	 * @access	public
	 * @return	array
	 */
	function get_count_todo_achievements($user_id)
	{
		$sql = "
				SELECT
					*
				FROM
					a_achievements_todo
				JOIN
					a_achievements ON a_achievements.id=a_achievements_todo.achievement_id
				JOIN
					a_achievement_categories ON a_achievement_categories.id=a_achievements.category_id
				WHERE
					a_achievements_todo.user_id = " . $user_id . "
				";
		$query = $this->db->query($sql);
		return $query->num_rows();
	}
	
	/**
	 * Deletes a new todo achievement
	 */
	function delete_todo_achievement($achievementId, $userId)
	{
		$this->db->delete($this->table_name, array('achievement_id' => $achievementId, 'user_id' => $userId));
	}
	
	/**
	 * Check if todo achievement exists
	 *
	 * @param	string
	 * @return	object
	 */
	function todo_achievement_exists($achievementId, $userId)
	{
		$this->db->where('achievement_id', $achievementId);
		$this->db->where('user_id', $userId);

		$query = $this->db->get($this->table_name);
		if ($query->num_rows() == 1) return true;
		return false;
	}
	

 }
 ?>